/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package actions;

import beans.ObraSocialBean;
import daos.Dao;
import daos.DaoFactory;
import java.lang.reflect.Method;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import ws.TurnosDisponiblesSerializable;

/**
 *
 * @author C
 */
public class MostrarListaTurnosDisponibleAction extends Action{

    @Override
    public void execute(HttpServletRequest request, HttpServletResponse response) {
        HttpSession sesion = request.getSession(true);
        Class serviceClass, portClass;
        Object service, port;
        Method metGetPort, metGetTurno;       
        String os = (String) sesion.getAttribute("nom_os");
        String prestadora = request.getParameter("selectPrestadora");
        
        try {     
            Dao dao = DaoFactory.getDao("ObraSocial");             
            ObraSocialBean osBean=  (ObraSocialBean) dao.get(new ObraSocialBean(os));       
            
            serviceClass = Class.forName("wso."+osBean.getAccesoWS()+"_Service");
            portClass = Class.forName("wso." +osBean.getAccesoWS());
            service = serviceClass.newInstance();     
                  
            metGetPort    = serviceClass.getMethod("get"+osBean.getAccesoWS()+"Port", (Class[]) null);
            Class[] cArg = new Class[1];
            cArg[0] = String.class;   
            port   = (Object) metGetPort.invoke(service, (Object[]) null);         
            metGetTurno = portClass.getMethod("buscarTurnosPrestadora", cArg);       
            List <TurnosDisponiblesSerializable> lista = (List <TurnosDisponiblesSerializable>) metGetTurno.invoke(port,prestadora);
             
            
            request.setAttribute("listaPrestadoras", sesion.getAttribute("listaPrestadoras"));                   
            request.setAttribute("listaTurnos", lista); 
             
        } catch (Exception ex) { 
            request.setAttribute("Error", ex.getMessage());
            System.out.println("Error    " + ex.getMessage());
        }
        finally {
            this.gotoPage("/buscarturno.jsp", request, response);
        }    
    }    
}
